#ifndef ESP8266_H
#define ESP8266_H

#include <QSerialPort>
#include <QTimer>

#define  TOPIC_LED "led002"
#define  TOPIC_FAN "fan003"
#define  TOPIC_H    "H004"
#define  TOPIC_T    "T004"
#define  TOPIC_S    "S004"
#define  TOPIC_SUN  "Sun004"
#define  TOPIC_W    "W009"

#define MAX_MSG_SIZE 1024

#define  UID   "f0266ac8a48c97c96ad42822d06864e5"  //用户密钥

class ESP8266 : public QSerialPort
{
    Q_OBJECT
public:
    static ESP8266 *get()
    {
        static ESP8266 s;
        return &s;
    }

    explicit ESP8266(QObject *parent = nullptr);
    bool tcpConnect = false;

    QList<QByteArray> list;
    QByteArray response;
    //QByteArray packet;
    void subscribe();
    void sendDataToServer(const char *topic, const QByteArray &data);
public slots:
    void handleReadyRead();

    void handleError(QSerialPort::SerialPortError error);

private:
    enum Command { ATCommand, STACmd, ConnectAPCmd, ChecAPCmd, CheckConnectionCmd, CheckTcpCmd, SendDataLengthCmd };
    Command currentCommand;

    void sendATCommand();

    void sendSTACmd();

    void sendConnectAPCmd();

    void sendGetIPCmd();

    void establishTCPConnection();

    void handleConnected();


    void sendCheckConnectionCmd();
signals:
    void sigData(QByteArray data);

};

#endif // ESP8266_H
